package com.welsy.iter.business
{
	import com.adobe.cairngorm.business.ServiceLocator;
	import com.welsy.iter.model.IterModelLocator;
	import com.welsy.iter.view.itercomponents.IterElementEditView;
	import com.welsy.iter.vo.IterComponentsVO;
	import com.welsy.iter.vo.IterVO;
	import com.welsy.iter.vo.iter.IterElementVO;
	import com.welsy.iter.vo.iter.pattern.ICourseIterator;
	
	import flash.events.MouseEvent;
	import flash.net.*;
	
	import mx.containers.*;
	import mx.events.*;
	import mx.rpc.AbstractOperation;
	import mx.rpc.soap.*;
		
	public class IterDao
	{
		private var myddu:DragAndDropUtils;	
		
		[Bindable]
		public var model:IterModelLocator = IterModelLocator.getInstance();	
		
		public function IterDao ()
        {
        	myddu = new DragAndDropUtils();
        }
		
		public function getIterComponents (area:VBox):void
		{	
			area.removeAllChildren();
			var iterator:ICourseIterator = model.selectedIter.iterator();

                  
			while(iterator.hasNext()) {				
		    	//iterElement
		    	var iterElementVO:IterElementVO;
		    	iterElementVO = IterElementVO(iterator.next())

		    	var iterElementView:IterElementEditView = new IterElementEditView
		    	iterElementView.elementIter = iterElementVO
		    	
		    	iterElementView.addEventListener(DragEvent.DRAG_ENTER, myddu.doDragEnter);
		        iterElementView.addEventListener(DragEvent.DRAG_DROP, myddu.doDragDrop);
		    	iterElementView.addEventListener(MouseEvent.MOUSE_MOVE, myddu.dragIt);
		    	                
            	area.addChild(iterElementView);                 	    		
       		}	
		}
		        
        public function saveIter(area:VBox):void {
        	
        	//get iter obj from model
        	var iter:IterVO = model.selectedIter.iter; 
        	
        	var requestStructTemp:String
        	var requestStruct:String = "<SaveIterComponents xmlns='http://Welsy.ServiceContracts/2007/02'>"; 
        	requestStruct += "<ns1:SaveIterComponentsRequest xmlns:ns1='http://tempuri.org/IterDataTypes.xsd'>";
			requestStruct += "<ns1:Iter><ns1:IterId>" + iter.id + "</ns1:IterId>";
			requestStruct += "<ns1:ParentIterId>" + iter.parentIterId + "</ns1:ParentIterId>";
			requestStruct += "<ns1:Title>" + iter.title + "</ns1:Title>";
			requestStruct += "<ns1:Description>" + iter.description + "</ns1:Description>";
			requestStruct += "<ns1:Credits>" + iter.credits + "</ns1:Credits>";
			requestStruct += "<ns1:WebLink>" + iter.webLink + "</ns1:WebLink>";
			requestStruct += "<ns1:Note>" + iter.notes + "</ns1:Note>";
			requestStruct += "<ns1:State>" + iter.state + "</ns1:State>";						
			requestStruct += "<ns1:AuthorId>" + iter.authorId + "</ns1:AuthorId>";			
			
			requestStructTemp = "</ns1:Iter><ns1:IterElements>";
        	
        	var myElementsVector:Array = area.getChildren();
        	var iterElementPos:int = 1;        	
        	var sumCreditsNumber:int = 0;
        	        	
        	var ic:IterComponentsVO = new IterComponentsVO;       	

        	for (var i:int = 0; i < myElementsVector.length; i++) {
				var myIterElement:Array = myElementsVector[i].getChildren();

				if (myIterElement.length > 1){
	        		var iterElement:IterElementVO = IterElementEditView(myElementsVector[i]).elementIter;
	        		
	        		if (iterElement.iterElementId == null || iterElement.iterElementId == "")
	        			iterElement.iterElementId = "0";
	        		
	        		iterElement.position = iterElementPos.toString();
	        		requestStructTemp += "<ns1:IterElementType>";
	        		requestStructTemp += "<ns1:IterElementId>" + iterElement.iterElementId + "</ns1:IterElementId>";
				    requestStructTemp += "<ns1:Title>" + iterElement.title + "</ns1:Title>";
				    requestStructTemp += "<ns1:Description>" + iterElement.Description + "</ns1:Description>";
				    requestStructTemp += "<ns1:ParentIterElementId>" + iterElement.ParentIterElementId + "</ns1:ParentIterElementId>";
					requestStructTemp += "<ns1:CourseId>" + iterElement.CourseId + "</ns1:CourseId>";
					requestStructTemp += "<ns1:FileId>" + iterElement.fileId + "</ns1:FileId>";
					requestStructTemp += "<ns1:FileName>" + iterElement.fileName + "</ns1:FileName>";
					requestStructTemp += "<ns1:CourseOnSiteId>" + iterElement.CourseOnSiteId + "</ns1:CourseOnSiteId>";
					requestStructTemp += "<ns1:CreditsNumber>" + iterElement.CreditsNumber + "</ns1:CreditsNumber>";
					requestStructTemp += "<ns1:Type>" + iterElement.type + "</ns1:Type>";
				    requestStructTemp += "<ns1:Position>" + iterElementPos + "</ns1:Position>";				    
				    requestStructTemp += "<ns1:Priority>" + iterElement.Priority + "</ns1:Priority>";
				    requestStructTemp += "</ns1:IterElementType>";
					
					sumCreditsNumber = sumCreditsNumber + int(iterElement.CreditsNumber)
					
					//costruisce il IterComponentsVO
					//add iterElement to the IterComponentsVO obj
	                ic.addItem(iterElement);

					iterElementPos++;									
				}
	        }
	        
			iter.creditsNumber = sumCreditsNumber.toString()
			
	        //creditsNumber dell'iter
	        requestStruct += "<ns1:CreditsNumber>" + iter.creditsNumber + "</ns1:CreditsNumber>";			
	        requestStruct += requestStructTemp

	        ic.iter = iter
	        
	        requestStruct += "</ns1:IterElements></ns1:SaveIterComponentsRequest>";
	        requestStruct += "</SaveIterComponents>";

	        var myInsertionRequest:XML = new XML (requestStruct);	        
	        
	        //todo refactoring
	        var ws : WebService = ServiceLocator.getInstance().getWebService("iterServices") as WebService
	        executeService (ws, "SaveIterComponents", myInsertionRequest); 
	        
	        //update model
	        model.selectedIter = ic;
        }
        
        //todo da togliere
        private function executeService (wsBlogAggr:WebService, opName:String, message:XML) : void
        {
        	var myAbstractOperation:AbstractOperation = wsBlogAggr.getOperation(opName);
        	var myOperation:Operation = Operation (myAbstractOperation);
        	myOperation.request = message;   
        	wsBlogAggr[opName].send ();
        }
	}
}